"""
Case Type   : 基础功能-兼容B库语法-optimize
Case Name   : optimize间隔分区表
Create At   : 2024/10/28
Owner       : peilinqian
Description :
    step1:创建测试表并插入数据
    step2:创建索引
    step3:删除表中数据
    step4:设置enable_defer_calculate_snapshot为off，optimize表
Expect      :
    step1:创建测试表并插入数据 expect: 创建插入成功
    step2:创建索引 expect: 创建成功
    step3:删除表中数据 expect:成功
    step4:设置enable_defer_calculate_snapshot为off，optimize表 expect:表回收成功
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.constant = Constant()
        self.com = Common()
        self.t_name = 't_compatibility_optimize_0013'

    def test_main(self):
        init_para = self.com.show_param("enable_defer_calculate_snapshot")
        step_txt = '----step1:创建测试表并插入数据 expect: 创建插入成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name};" \
                     f"create table {self.t_name} (c1 date,c2 numeric,c3 text)" \
                     f"partition by range(c1) interval('1 day')" \
                     f"(partition p1 values less than('2020-12-12 00:00:00')," \
                     f" partition p2 values less than('2022-02-02 00:00:00'));" \
                     f"insert into {self.t_name} values('2020-12-12',3,'嘿');" \
                     f"insert into {self.t_name} values('2022-12-13',3,'嘿');" \
                     f"insert into {self.t_name} values('2022-12-14',3,'嘿');"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn('INSERT', create_result, '执行失败:' + step_txt)

        step_txt = '----step2:创建索引 expect: 创建成功----'
        self.log.info(step_txt)
        create_sql = f"create index test_index_013 on {self.t_name} " \
                     f"using btree(c1);"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, create_result,
                      '执行失败:' + step_txt)

        step_txt = '----step3:删除表中数据 expect:成功----'
        self.log.info(step_txt)
        sql = f'delete from {self.t_name};'
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, result,
                      '执行失败:' + step_txt)

        step_txt = '----step4:设置enable_defer_calculate_snapshot为off，' \
                   'optimize表 expect:表回收成功---'
        self.log.info(step_txt)
        sql = f'alter system set enable_defer_calculate_snapshot = off;' \
              f'select pg_sleep(10);' \
              f'optimize verbose table public.{self.t_name};' \
              f'select pg_sleep(1);' \
              f'optimize verbose table public.{self.t_name};' \
              f'select pg_sleep(1);' \
              f'optimize verbose table public.{self.t_name};' \
              f'alter system set enable_defer_calculate_snapshot={init_para};'
        optimize_result = self.pri_sh.execut_db_sql(sql)
        self.log.info(optimize_result)
        suc_msg = 'found 1 removable, 0 nonremovable row versions'
        self.assertEqual(optimize_result.count(suc_msg), 3,
                         '执行失败:' + step_txt)
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, optimize_result,
                      '执行失败:' + step_txt)
        self.assertEqual(2, optimize_result.count(
            self.constant.alter_system_success_msg), '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----step:清理数据; expect:清理成功----'
        self.log.info(step_txt)
        drop_sql = f"drop table if exists {self.t_name};"
        result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(result)

        self.log.info('----teardown断言----')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result,
                      "执行失败" + step_txt)

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
